const Hapi = require('hapi')
const HapiAuthJwt2 = require('hapi-auth-jwt2')
const config = require('./config')
const routesHelloHapi = require('./routes/hello-hapi')
const routesShops = require('./routes/shops')
const routesOrders = require('./routes/orders')
const routesUsers = require('./routes/users')
// 引入自定义的 hapi-swagger 插件配置
const pulginHapiSwagger = require('./plugins/hapi-swagger')
const pulginHapiPagination = require('./plugins/hapi-pagination')
const pulginHapiAuthJwt2 = require('./plugins/hapi-auth-jwt2')


const server = new Hapi.Server();
server.connection({
    port: config.port,
    host: config.host,
})

const init = async () => {
    // 为系统使用 hapi-swagger
    await server.register([
        ...pulginHapiSwagger,
        pulginHapiPagination,
        HapiAuthJwt2,
    ])
    pulginHapiAuthJwt2(server)

    server.route([
        ...routesHelloHapi,
        ...routesShops,
        ...routesOrders,
        ...routesUsers,
    ])

    await server.start()
    console.info(`Server running at: ${server.info.uri}`)
}

init();